package com.service.impl;

import com.dao.CourseMapper;
import com.dao.WorkMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pojo.Course;
import com.pojo.Work;
import com.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class WorkServiceImpl implements WorkService {
    private final CourseMapper courseMapper;
    private final WorkMapper workMapper;

    @Autowired(required = false)
    public WorkServiceImpl(CourseMapper courseMapper, WorkMapper workMapper){
        this.courseMapper = courseMapper;
        this.workMapper = workMapper;
    }

    @Override
    public String createWork(String courseId, String name) {
        List<String> workList;
        Course course;
        Work work = new Work();
        Gson gson = new Gson();

        String id = String.valueOf(workMapper.getCount() + 1);

        try {
            course = courseMapper.selectCourseByID(courseId).get(0);
        } catch (Exception e){
            return "can not find the course";
        }

        // 如果原来有数据就读取，否则新建链表
        if (course.getHomeworkId() != null){
            try {
                workList = gson.fromJson(course.getHomeworkId(), new TypeToken<ArrayList<String>>(){}.getType());
            } catch (Exception e){
                return "can not read the work list";
            }
        } else {
            workList = new ArrayList<>();
        }


        // 向课程中添加信息
        workList.add(id);
        course.setHomeworkId(gson.toJson(workList));

        // 添加作业
        work.setId(id);
        work.setCourseId(courseId);
        work.setName(name);
        try {
            workMapper.insertWork(work);
        } catch (Exception e){
            return "can not add new work";
        }

        // 更新课程
        try {
            courseMapper.updateCourseById(course);
        } catch (Exception e){
            return "can not update course";
        }

        return "ok";
    }

    @Override
    public String deleteWork(String id, String courseId) {
        List<String> workList;
        Course course;
        Gson gson = new Gson();

        try {
            course = courseMapper.selectCourseByID(courseId).get(0);
        } catch (Exception e){
            return "can not find the course";
        }

        // 如果原来有数据就读取，否则新建链表
        if (course.getHomeworkId() != null){
            try {
                workList = gson.fromJson(course.getHomeworkId(), new TypeToken<ArrayList<String>>(){}.getType());
            } catch (Exception e){
                return "can not read the work list";
            }
        } else {
            return "there is no any work";
        }


        // 在课程中删除作业信息
        workList.remove(id);
        course.setHomeworkId(gson.toJson(workList));


        try {
            workMapper.delWorkById(id);
        } catch (Exception e){
            return "can not delete work";
        }

        // 更新课程
        try {
            courseMapper.updateCourseById(course);
        } catch (Exception e){
            return "can not update course";
        }

        return "ok";
    }

    @Override
    public List<Work> findWorkByCourseId(String courseId) {
        return workMapper.selectWorkByCourseId(courseId);
    }
}
